package com.douqu.game.core.config.map;

import com.douqu.game.core.config.common.CommonData;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.CoreUtils;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-04-13 10:07
 */
public class CityConfig extends GameObject {

    public int camp;

    /**
     1 主城
     2 一般城
     3 关卡
     4 镇
     */
    public int type;
    /** 是否能战斗 */
    public boolean canBattle;
    /** 是否能招兵(是否能复活) */
    public boolean canConscript;

    /**
     * 驻军类型
     * 对应CityArmy的type
     */
    public int stationedArmy;

    /**
     * 军队上限
     */
    public int armyMaxCount;

    /**
     * 首次占领掉落资源
     */
    public GoodsData[] dropAssets;

    /**
     * 阵营权重
     */
    public CommonData[] areaRelateWeight;

    /**
     * 关联的城的ID
     */
    public int[] connCityIds;

    @Override
    public boolean check()
    {
        if(dropAssets == null || areaRelateWeight == null || connCityIds == null)
        {
            LogUtils.error("Load CityConfig check error -> id:" + id);
            return false;
        }

        for(int cityId : connCityIds)
        {
            if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.CITY_KEY, cityId) == null)
            {
                LogUtils.error("Load CityConfig connCityIds check error city is null -> cityId:" + cityId + ", id:" + id);
                return false;
            }
        }
        return super.check();
    }

    /**
     * 是否能到达
     * @param target
     * @return
     */
    public boolean isGo(int target)
    {
        for(int city : connCityIds)
        {
            if(city == target)
                return true;
        }
        return false;
    }


    @Override
    public boolean setVariable(String key, String value)
    {
        if("dropAssets".equals(key))
        {
            if(CoreUtils.isErrorValue(value))
            {
                dropAssets = new GoodsData[0];
                return true;
            }
            else
            {
                dropAssets = LoadUtils.loadGoodDataToArray(key, value);
                return LoadUtils.checkGoods(dropAssets, getClass(), key, id);
            }
        }
        else if("areaRelateWeight".equals(key))
        {
            areaRelateWeight = LoadUtils.loadCommonDataToArray(key, value);
            for(CommonData campData : areaRelateWeight)
            {
                if(SGCommonProto.E_CAMP_TYPE.forNumber(campData.id) == null)
                {
                    LogUtils.error("Load CityConfig areaRelateWeight error :" + value + " id:" + id);
                    return false;
                }
            }
            return true;
        }
        else if("connCityIds".equals(key))
        {
            connCityIds = LoadUtils.loadIntArray(key, value);
            return true;
        }
        else
            return super.setVariable(key, value);
    }

    public int getAreaWeight(int camp){
        for (CommonData commonData:areaRelateWeight){
            if (camp == commonData.id){
                return commonData.value;
            }
        }
        return 0;
    }
}
